package cn.edu.swu.shopcar;

import cn.edu.swu.book.BOOK;
import cn.edu.swu.book.bookRepo;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;
import java.sql.SQLException;

@WebServlet("/shoppingServlet")
public class shoppingServlet extends HttpServlet {
     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method=req.getParameter("method");
        Integer id= Integer.valueOf(req.getParameter("bookId"));
         System.out.println(method);
         System.out.println(id);
        if("add".equals(method)){
            try {
                add(req,resp);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
         if("delete".equals(method)){
             try {
                 delete(req,resp);
             } catch (SQLException e) {
                 throw new RuntimeException(e);
             }
         }
         if("decrease".equals(method)){
             try {
                 decrease(req,resp);
             } catch (SQLException e) {
                 throw new RuntimeException(e);
             }
         }
         if("increase".equals(method)){
             try {
                 increase(req,resp);
             } catch (SQLException e) {
                 throw new RuntimeException(e);
             }
         }
    }

    private void add(HttpServletRequest req,HttpServletResponse resp) throws SQLException, IOException, ServletException {
        HttpSession session=req.getSession(true);
        BOOK book=new BOOK();
        book= bookRepo.getInstance().getBookById(req.getParameter("bookId"));
        shoppingCar shoppingCar= (cn.edu.swu.shopcar.shoppingCar) session.getAttribute("shoppingCar");
        if(shoppingCar==null){
            shoppingCar=new shoppingCar();
            session.setAttribute("shoppingCar",shoppingCar);
        }
        shoppingCar.add(book);

        resp.sendRedirect("./cart.html");

    }

    private void delete(HttpServletRequest req,HttpServletResponse resp) throws SQLException, IOException, ServletException {
        HttpSession session=req.getSession(false);
        BOOK book=new BOOK();
        book= bookRepo.getInstance().getBookById(req.getParameter("bookId"));
        shoppingCar shoppingCar= (cn.edu.swu.shopcar.shoppingCar) session.getAttribute("shoppingCar");
        if(shoppingCar==null){
            shoppingCar=new shoppingCar();
            session.setAttribute("shoppingCar",shoppingCar);
        }
        shoppingCar.delete(book.getId());

        resp.sendRedirect("./cart.html");

    }

    private void decrease(HttpServletRequest req,HttpServletResponse resp) throws SQLException, IOException, ServletException {
        HttpSession session=req.getSession(false);
        BOOK book=new BOOK();
        book= bookRepo.getInstance().getBookById(req.getParameter("bookId"));
        shoppingCar shoppingCar= (cn.edu.swu.shopcar.shoppingCar) session.getAttribute("shoppingCar");
        if(shoppingCar==null){
            shoppingCar=new shoppingCar();
            session.setAttribute("shoppingCar",shoppingCar);
        }
        shoppingCar.decrease(book.getId());
        resp.sendRedirect("./cart.html");
    }

    private void increase(HttpServletRequest req,HttpServletResponse resp) throws SQLException, IOException, ServletException {
        HttpSession session=req.getSession(false);
        BOOK book=new BOOK();
        book= bookRepo.getInstance().getBookById(req.getParameter("bookId"));
        shoppingCar shoppingCar= (cn.edu.swu.shopcar.shoppingCar) session.getAttribute("shoppingCar");
        if(shoppingCar==null){
            shoppingCar=new shoppingCar();
            session.setAttribute("shoppingCar",shoppingCar);
        }
        shoppingCar.increase(book.getId());
        resp.sendRedirect("./cart.html");
    }
}
